package com.honey.compilation.sql.dml;

import com.honey.compilation.sql.SqlKeyWord;
import com.honey.compilation.sql.SqlScript;
import com.honey.compilation.sql.SqlTable;
import com.honey.core.builder.CharacterBuilder;
import com.honey.core.builder.CharacterBuilderFactory;
import com.honey.core.compiler.CompilationException;

/**
 * 删除命令 Delete Form tableName
 * @author Administrator
 *
 */
public  class Delete extends AbstractDelete implements SqlScript{
	
	/** 数据表的名称 */
	private SqlTable table;
	
	/** */
	private Condition condition ;
	
	/**
	 * 构造函数
	 * @param tableName 表
	 */
	public Delete(String tableName){
		this.table =new SqlTable(tableName);
	}
	
	/**
	 * 构造函数
	 * @param schema 数据库结构
	 * @param tableName 表名称
	 */
	public Delete(String schema ,String tableName){
		this.table =new SqlTable(schema,tableName);
	}
	
	
	/**
	 * 构造函数
	 * @param table 表
	 */
	public Delete(SqlTable table){
		this.table = table ;
	}
	
	/**
	 * 构造函数
	 * @param table 表
	 * @param condition 条件
	 */
	public Delete(SqlTable table,Condition condition ){
		this.table = table;
		this.condition = condition ;
	}
	
	/**
	 * 设定数据库结构
	 * @param schema
	 */
	public void setSchema(String schema){
		this.table.setSchema(schema);
	}
	
	/**
	 * 获取数据结构
	 * @return
	 */
	public String getSchema(){
		return this.table.getSchema();
	}
	
	/**
	 * 获取数据库名称
	 * @return
	 */
	public String getTableName(){
		return this.table.getTableName();
	}
	
	/**
	 * 条件
	 * @return
	 */
	public Condition getCondition() {
		return condition;
	}
	
	/**
	 * 条件
	 * @param condition
	 */
	public void setCondition(Condition condition) {
		this.condition = condition;
	}

	/*
	 * (non-Javadoc)
	 * @see com.honey.core.dom.sql.dml.AbstractDelete#compiledContent(int)
	 */
	@Override
	public CharacterBuilder compiledContent(int indentLevel )throws CompilationException {
		CharacterBuilder answer = CharacterBuilderFactory.createC16StringBuilder();
		answer.append(super.compiledContent(indentLevel))
		.append(table.compiledContent(0));
		if(condition != null){
			condition.setEnableAlias(false);
			answer.append(SqlKeyWord.WHERE)
			.append(condition.compiledContent(indentLevel))
			;
		}
		answer.append(SqlKeyWord.OPERATION_SEMICOLON);
		
		return answer;
	}

}
